package me.zixing.memorandum.utils;

import me.zixing.memorandum.domain.DayDO;
import me.zixing.memorandum.domain.DayTask;
import me.zixing.memorandum.ui.component.TaskPanel;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class TaskPanelUtils {
    public static void refreshTasksPanel(JViewport jViewport, DayDO dayDO){
        List<DayTask> dayTasks = JdbcUtils.getDayTasks(dayDO);
        JPanel memMainPanel = new JPanel();

        if(dayTasks.size() != 0){
            memMainPanel.setSize(280,dayTasks.size() * 90);
            memMainPanel.setLayout(null);
            memMainPanel.setPreferredSize(new Dimension(memMainPanel.getWidth(),memMainPanel.getHeight()));
            memMainPanel.setBackground(new Color(230,230,230));
            int i = 0;
            for (DayTask dayTask:dayTasks) {
                TaskPanel taskPanel = new TaskPanel(dayTask);
                taskPanel.setBounds(10,10 +i * 90,265,80);
                memMainPanel.add(taskPanel);
                i++;
            }
        }else{
            memMainPanel.setSize(280,400);
            memMainPanel.setLayout(null);
            memMainPanel.setPreferredSize(new Dimension(memMainPanel.getWidth(),memMainPanel.getHeight()));
            JLabel nullLabel = new JLabel();
            nullLabel.setBounds(90,100,128,128);
            nullLabel.setIcon(new ImageIcon(TaskPanel.class.getClassLoader().getResource("null.png")));
            memMainPanel.add(nullLabel);
        }

        jViewport.remove(0);
        jViewport.add(memMainPanel);

    }
}
